package com.mobileares.midp.widgets.client.panel;

import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyDownEvent;
import com.google.gwt.event.dom.client.KeyDownHandler;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FocusPanel;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.Widget;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: Tom
 * Date: 2011-7-21
 * Time: 10:51:24
 * To change this template use File | Settings | File Templates.
 */
public class MaxPanel extends Composite {
    private FocusPanel container;
    private List<IResume> listeners = new ArrayList<IResume>();
    private Widget widget;

    public MaxPanel() {
        container = new FocusPanel();
        container.setStyleName("max-pop");
        initWidget(container);
        container.addKeyDownHandler(new KeyDownHandler() {
            public void onKeyDown(KeyDownEvent keyDownEvent) {
              if(KeyCodes.KEY_ESCAPE == keyDownEvent.getNativeKeyCode()){
                  resume();
              }
            }
        });
        setTitle("按【Esc】键复原");
    }
    public void show(){
        RootPanel.get().add(this);
        container.setFocus(true); 
    }

    public void setMaxWidget(Widget widget) {
        container.setWidget(widget);
        this.widget = widget;
    }
    public void resume(){
         for(IResume l:listeners){
             l.process(widget);
         }
        this.removeFromParent();
    }

    public void addResumeListener(IResume listener){
         listeners.add(listener);
    }
    public interface  IResume{
        void process(Widget widget);
    }
}
